package com.firefly.web.controller.admin.ums;


import com.firefly.web.controller.admin.ConsoleApiRestController;
import com.firefly.service.command.ums.MenuCommand;
import com.firefly.service.query.ums.MenuQuery;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.dto.ums.MenuDto;
import com.firefly.dao.dto.ums.MenuSortDto;
import com.firefly.dao.vo.ums.BreadCrumbVo;
import com.firefly.dao.vo.ums.MenuVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@ConsoleApiRestController("menu")
public class MenuController {
    private final MenuQuery menuQuery;
    private final MenuCommand menuCommand;
    @Autowired
    public MenuController(MenuQuery menuQuery, MenuCommand menuCommand) {
        this.menuQuery = menuQuery;
        this.menuCommand = menuCommand;
    }
    //	获取菜单路径
    @GetMapping("breadcrumb")
    public List<BreadCrumbVo> getBreadCrumb(@RequestParam String url) {
        return menuQuery.breadCrumb(url);
    }
    //	左侧权限菜单
    @GetMapping("list")
    public List getList(HttpServletRequest httpRequest) throws JsonProcessingException {
        return menuQuery.leftMenus(httpRequest.getParameter("fireflyUserMap"));
    }
    //	获取菜单树
    @GetMapping("tree")
    public Object getTree() {
        return menuQuery.listTree();
    }
    //	获取下拉菜单树
    @GetMapping("select")
    public Object getSelect() {
        return menuQuery.selectTree();
    }
    @GetMapping("detail")
    public MenuVo postDetail(@Validated(CommonDto.showDetail.class) CommonDto request) {
        return menuQuery.detail(request);
    }
    @PostMapping("create")
    public boolean postCreate(@Validated(MenuDto.create.class) MenuDto request) {
        return menuCommand.create(request);
    }
    @PostMapping("update")
    public boolean postUpdate(@Validated(MenuDto.update.class) MenuDto request) {
        return menuCommand.update(request);
    }
    @PostMapping("remove")
    public Object postRemove(@Validated(CommonDto.removeDetail.class) CommonDto request) {
        return menuCommand.removeById(request.getDetailId());
    }
    @PostMapping("sort")
    public boolean postSort(@Validated MenuSortDto request) {
        return menuCommand.sort(request);
    }
}

